package com.wyl.wyl.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.wyl.wyl.testapplication.R;
import com.wyl.wyl.testapplication.bean.CarMainBean;
import com.wyl.wyl.testapplication.bean.LocationBean;

import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/8/2 15:50
 * @email siyu.wang@inossem.com
 */
public class LocationListAdapter extends BaseAdapter {
    private Context mContext;
    private List<CarMainBean.BodyBean.WhListBean> mLocationBeanList;
    private LayoutInflater inflater;
    private LocationListAdapterListener listener;

    public LocationListAdapter(Context context, List<CarMainBean.BodyBean.WhListBean> locationBeanList, LocationListAdapterListener listener) {
        mContext = context;
        mLocationBeanList = locationBeanList;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mLocationBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return mLocationBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CarMainBean.BodyBean.WhListBean inventoryAddressItem = mLocationBeanList.get(position);
        LocationListAdapter.ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_in_storage_select, null);
            viewHolder = new LocationListAdapter.ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (LocationListAdapter.ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, inventoryAddressItem);
        return convertView;
    }

    private void initView(LocationListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.text = (TextView) view.findViewById(R.id.text);
        viewHolder.value = (TextView) view.findViewById(R.id.value);
    }

    private void initData(Context context, LocationListAdapter.ViewHolder viewHolder, View view, final CarMainBean.BodyBean.WhListBean item) {
        viewHolder.text.setText(item.getWh_code() + " - " + item.getWh_name());
        viewHolder.value.setText(item.getWh_id());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelect(item.getWh_code(), item.getWh_name(), item.getWh_id());
            }
        });
    }

    private class ViewHolder {
        private TextView text, value;
    }

    public static abstract class LocationListAdapterListener {
        public abstract void onSelect(String code, String name, String id);
    }
}
